package liuyu_test2;

//import static org.junit.jupiter.api.Assertions.*;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
import java.util.Date;
public class Charge {
    public static void main(String [] args) throws ParseException {
        Calendar cal1,cal2;
        double money;
        int flag = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入开始时间：");
        String Bdate = sc.nextLine();

        System.out.println("请输入截止时间：");
        String Edate  = sc.nextLine();
        cal1 = getdatefangfa(Bdate);
        cal2 = getdatefangfa(Edate);
        //public String classify(int a, int b, int c) {classify(cal1,cal2);
        //计算开始时间与截止时间的差值
        SimpleDateFormat dfs = new SimpleDateFormat("HH:mm:ss");

        Date begin=dfs.parse(Bdate.substring(12,19));
        Date end = dfs.parse(Edate.substring(12,19));
        long between=(end.getTime()-begin.getTime())/1000;
        long min=between/60;
        //不足一分钟的按一分钟计算
        if(between%60>0){
            min=min+1;
        }
        //System.out.println("时间"+min);
        int week;
        //调用testweek函数判断为星期几
        week=Phonebill.testweek(cal1.get(Calendar.YEAR),cal1.get(Calendar.MONTH) + 1,cal1.get(Calendar.DAY_OF_MONTH));
        //System.out.println("周"+week);
        //当前时间为星期天时，考虑特殊情况
        if(week==7){
            if(cal1.get(Calendar.HOUR_OF_DAY)<2||cal2.get(Calendar.HOUR_OF_DAY)>=3){
                /*当时间在2:00到3:00转换的区域内时，
                 * 调用testdate1函数进一步判断是否为转换时间,
                 * 并对通话时间进行相应处理
                 */
                flag=Phonebill.testdate1(cal1.get(Calendar.MONTH) + 1,cal1.get(Calendar.DAY_OF_MONTH));
                if(flag == 1){
                    min -= 60;
                }
            }
            if(cal1.get(Calendar.HOUR_OF_DAY)<=2||cal2.get(Calendar.HOUR_OF_DAY)>=2){
                /*当时间在2:59:59到2:00转换的区域内时，
                 * 调用teatdate2函数进一步判断是否为转换时间，
                 * 并对通话时间进行相应处理
                 */
                flag = Phonebill.testdate2(cal1.get(Calendar.MONTH) + 1,cal1.get(Calendar.DAY_OF_MONTH));
                if(flag == 1){
                    min += 60;
                }
            }
            //此处输出一下计算出来的时间，看是否正确
            //System.out.println("时间"+min);
            if(min<=20) {
//当通话时间不超过20分钟时，计算费用
                money=0.05*min;
            }
            else {
//当通话时间超过20分钟时，计算费用
                money=1+0.1*min;
            }
//将double类型的money的值保留小数点后三位输出
            DecimalFormat df = new DecimalFormat("0.000");
            String mmoney = df.format(money);
            System.out.printf("电话费用一共为:"+mmoney);
        }
    }
    private static Calendar getdatefangfa(String date){
        String dateStr = date;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        Date dt = null;
        try {
            dt = sdf.parse(dateStr);
            cal.setTime(dt);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return cal;
    }

}
